
var dxmOrderObj = {
    /**
     * 描述 : 操作指定类型的分页
     * 参数 : type : 分页类型
     *       mode : 操作模式, 默认=搜索, 其它=分页操作
     * 作者 : Zoro.Zhu
     */
    'paging': function (type, mode) {
        var paging = document.getElementById(type);                             //分页对象
        paging.paging(mode);
    },

    /**
     * 描述 : 搜索分页
     * 作者 : Zoro.Zhu
     */
    'search': function (sort) {
        $('table[name=pagingBlock]').attr('page', 1);
        var args = dxmOrderObj.getSearch();
        var data = {'search': args};
        if(sort !== ''){
            var ascClass = $('#' + sort + 'Sort').attr('class');
            //复原其它排序
            $('.of-paging_sort_def').each(function(){
                $(this).attr('class', 'of-paging_sort_def');
            });
            if(ascClass === 'of-paging_sort_def' || ascClass === 'of-paging_sort_def of-paging_sort_desc'){
                $('#' + sort + 'Sort').attr('class', 'of-paging_sort_def of-paging_sort_asc');
                sort += ' ASC';
            } else if (ascClass === 'of-paging_sort_def of-paging_sort_asc'){
                $('#' + sort + 'Sort').attr('class', 'of-paging_sort_def of-paging_sort_desc');
                sort += ' DESC';
            }
            data['sort'] = sort;
        }
        dxmOrderObj.paging('dxmOrderList', data);                  //搜索内容
    },

    /**
     * 描述 : 获取搜索条件
     * 作者 : Zoro.Zhu
     */
    'getSearch': function () {
        var args = { 
            orderId: $("#orderId").val(),
            orderState: $("#orderState").val(),
            trackDogStatus: $("#trackDogStatus").val(),
            parcelNum: $("#parcelNum").val(),
            country: $("#country").val(),
            logistics: $("#logistics").val(),
            contact: $("#contact").val(),
            pAccount: $("#pAccount").val(),
            trackNo: $("#trackNo").val(),
            pickingRemark: $("#pickingRemark").val(),
            singleExpress: $("#singleExpress").val(),
            paymentTimeStart: $("#paymentTimeStart").val(),
            paymentTimeEnd: $("#paymentTimeEnd").val(),
            grossStart: $("#grossStart").val(),
            grossEnd: $("#grossEnd").val(),
            grossMarginStart: $("#grossMarginStart").val(),
            grossMarginEnd: $("#grossMarginEnd").val(),
            costMarginStart: $("#costMarginStart").val(),
            costMarginEnd: $("#costMarginEnd").val(),
            logisticsTimeStart: $("#logisticsTimeStart").val(),
            logisticsTimeEnd: $("#logisticsTimeEnd").val(),
            printTimeStart: $("#printTimeStart").val(),
            printTimeEnd: $("#printTimeEnd").val(),
            costPriceTotalStart: $("#costPriceTotalStart").val(),
            costPriceTotalEnd: $("#costPriceTotalEnd").val(),
            sku: $("#sku").val(),
            dxmAccount: $("#dxmAccount").val(),
        };
        return args;
    },

    /**
     * 描述 : 修改采购信息
     * 作者 : Zoro.Zhu
     */
    "updPrice":function (orderId){
        if(orderId === ''){
            var ids = "";
            $("input[name='check[]']:checked").each(function () {
                ids += $(this).val() + ",";
            });
            if (ids === "") {
                layer.msg("请先勾选订单", {icon : 0});
                return false;
            }
            orderId = ids
        }
        layer.open({
            title: '修改采购信息',
            type: 2,
            area: ['80%', '60%'],
            fix: false,
            maxmin: true,
            btn: ['保存','关闭'],
            yes: function (index, layero) {
                $.ajax({
                    url: ROOT_URL + "/?c=ctrl_wish_dxmOrder&a=updPrice",
                    type: "POST",
                    data: layero.find('iframe').contents().find("#myform").serialize(),
                    success: function (result) {
                        result = JSON.parse(result);
                        if (result.state !== 200) {
                            layer.alert(result.info);
                            return false;
                        } else {
                            layer.close(index);
                            layer.alert(result.info);
                            $('table').get(0).paging('+0');
                            return false;
                        }
                    }
                });
            },
            btn2: function () {
                layer.closeAll();
            },
            content: ROOT_URL + '/?c=ctrl_wish_dxmOrder&a=updPriceView&orderId=' + orderId
        });
    },

    /**
     * 描述 ：按搜索条件导出数据
     * 作者 ：Zoro.Zhu
     * **/
    'dxmOrderExport' : function(){
        var args = dxmOrderObj.getSearch();
        window.location.href = ROOT_URL + '/index.php?c=ctrl_wish_dxmOrder&a=exportCsv' + '&orderId=' + args.orderId
            + '&orderState=' + args.orderState
            + '&trackDogStatus=' + args.trackDogStatus
            + '&parcelNum=' + args.parcelNum
            + '&country=' + args.country
            + '&logistics=' + args.logistics
            + '&contact=' + args.contact
            + '&pAccount=' + args.pAccount
            + '&trackNo=' + args.trackNo
            + '&pickingRemark=' + args.pickingRemark
            + '&singleExpress=' + args.singleExpress
            + '&paymentTimeStart=' + args.paymentTimeStart
            + '&paymentTimeEnd=' + args.paymentTimeEnd
            + '&grossStart=' + args.grossStart
            + '&grossEnd=' + args.grossEnd
            + '&grossMarginStart=' + args.grossMarginStart
            + '&grossMarginEnd=' + args.grossMarginEnd
            + '&costMarginStart=' + args.costMarginStart
            + '&costMarginEnd=' + args.costMarginEnd
            + '&logisticsTimeStart=' + args.logisticsTimeStart
            + '&logisticsTimeEnd=' + args.logisticsTimeEnd
            + '&printTimeStart=' + args.printTimeStart
            + '&printTimeEnd=' + args.printTimeEnd
            + '&costPriceTotalStart=' + args.costPriceTotalStart
            + '&costPriceTotalEnd=' + args.costPriceTotalEnd
            + '&dxmAccount=' + args.dxmAccount
            + '&sku=' + args.sku;
    },

    /**
     * 描述 : 标记打印完成
     * 作者 : Zoro.Zhu
     */
    'printOk' : function (account,orderId,logisticsId,parcelNum) {
        $.ajax({
            url: ROOT_URL + "/?c=ctrl_wish_dxmOrder&a=printOk",
            type: "POST",
            data: {account:account,orderId:orderId,logisticsId:logisticsId,parcelNum:parcelNum},
            success: function (result) {
                result = JSON.parse(result);
                if (result.code !== 200) {
                    layer.alert(result.info);
                    return false;
                } else {
                    pdfjsLib.GlobalWorkerOptions.workerSrc = ROOT_URL + '/view/js/pdfjs/build/pdf.worker.js';
                    var pdfData = atob(result.pdf);
                    var CMAP_URL = ROOT_URL + '/view/js/pdfjs/web/cmaps';
                    var loadingTask = pdfjsLib.getDocument({
                        data: pdfData,
                        cMapUrl: CMAP_URL,
                        cMapPacked: true,
                    });
                    // var loadingTask = pdfjsLib.getDocument({ url: result.url, });//  _url为后台请求pdf流的路径
                    loadingTask.promise.then(function(pdf) {
                        //此处适用pdf只有一个page情况
                        pdf.getPage(1).then(function(page) {
                            //scale调节生成的画布图片大小，与清晰度有关，需要调整。
                            var scale = 2;
                            var viewport = page.getViewport(scale);
                            //将生成的page对象加载到canvas中
                            var canvas = document.getElementById('thepdfcanvas2');
                            var context = canvas.getContext('2d');
                            canvas.height = viewport.height;
                            canvas.width = viewport.width;
                            canvas.style.width = "100%";
                            canvas.style.height = "100%";
                            var renderContext = {
                                canvasContext: context,
                                viewport: viewport
                            };
                            //此处开始画画布
                            page.render(renderContext).promise.then(function () {
                                var LODOP;
                                LODOP = getLodop();
                                LODOP.PRINT_INIT("面单标签");
                                LODOP.SET_PRINT_PAGESIZE(2, 1000, 1000, "User defined");
                                LODOP.SET_PRINT_MODE("FULL_WIDTH_FOR_OVERFLOW", true);
                                LODOP.SET_PRINT_MODE("FULL_HEIGHT_FOR_OVERFLOW", true);
                                //<canvas> 元素转换为.png图片的BASE64编码
                                var canvasEle = document.getElementById("thepdfcanvas2");
                                var dataUrl = canvasEle.toDataURL();
                                var html = '<img style="width: 1825px;height: 1800px;" src="' + dataUrl + '">';
                                LODOP.ADD_PRINT_HTM('0%','0%','100%','100%',html);
                                LODOP.PRINT();
                            });

                        });
                    });
                    // window.open("https://www.dianxiaomi.com/printMb.htm?packageIds=" + orderId + "&authId=" + logisticsId + "&isCos=1","_blank");
                    // layer.alert(result.info);
                    $('table').get(0).paging('+0');
                    return false;
                }
            }
        });
    },

    /**
     * 描述 : 查看物流信息
     * 作者 : Zoro.Zhu
     */
    'getLogisticsInfo' : function (trackNo) {
        layer.open({
            title: '物流信息查询',
            type: 2,
            area: ['50%', '80%'],
            fix: false,
            maxmin: true,
            btn: ['关闭'],
            btn2: function () {
                layer.closeAll();
            },
            content: 'https://ext.trackdog.com/s.htm?nums=' + trackNo + '&elementId=trackIframe-428580&width=800&height=600&type=2&utag=1509318&code=wishpost&lang=zh_CN&pf=wish&pf_c=WishPost'
        });
    },

    /**
     * 描述 : 查看快递信息
     * 作者 : Zoro.Zhu
     */
    'getSingleExpress' : function (singleExpress) {
        layer.open({
            title: '快递信息查询',
            type: 2,
            area: ['50%', '80%'],
            fix: false,
            maxmin: true,
            btn: ['关闭'],
            btn2: function () {
                layer.closeAll();
            },
            content: 'https://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=1&rsv_idx=1&tn=baidu&wd=' + singleExpress
        });
    },

    /**
     * 描述 : 重置下拉框
     * 作者 : Zoro.Zhu
     */
    'resetSelect' : function () {
        clearAllBtn('dxmAccount');
        clearAllBtn('orderState');
        clearAllBtn('trackDogStatus');
    },

};

L.data('paging.after[]', function() {
    if(this.data.length === 0) {
        var text = "总销售额: 0; 总平台佣金: 0; " +
            "总采购金额: 0; 总物流运费: 0; 总毛利: 0。";
    } else {
        var sumInfo = this.data[0].sumTotalPrice;
        console.log(sumInfo);
        var text = "总销售额: " + sumInfo['salesPrice'] + "; 总平台佣金: " + sumInfo['platPrice'] + "; " +
            "总采购金额: " + sumInfo['costPrice'] + "; 总物流运费: " + sumInfo['shipFee'] + "; 总退款金额: " + sumInfo['refundPrice'] + "; 总毛利: " + sumInfo['gross'] + "。";
    }
    $('#sumTotalPrice').text(text);
});


//pda扫描时按键扫描判断聚焦文本框
// $(document).keydown(function(event){
//     if(event.keyCode == 13){
//         dxmOrderObj.search('');
//     }
// });

$(function () {
    $("#singleExpress").focus();
    $('#dxmAccount').fSelect();
    $('#orderState').fSelect();
    $('#trackDogStatus').fSelect();
});